<?php
Box::$description["BoxBookNav"] = "Book navigator";
/**
*The simple navigator.
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Boxes
*@subpackage BoxBookNav
*/
class BoxBookNav extends Box
{
	private $cached_data = array();
	private $last_update = 0;

	public function __construct( $title )
	{
		parent::__construct( $title, "inline" );
		$this->SetStorage(array("path" => "",
							"delay" => 5000,
							"speed" => 2000,
							"width" => "auto",
							"height" => "auto" ) );
	}

	private function UpdateCache()
	{
		$books = AtomCache::GetAtoms("book", array());

		$tmp = $this->cached_data;
		foreach ( $books as $id => $book )
		{
			if ( !in_array( $id,  $tmp ) || ($this->last_update < $book->GetLastUpdate()) )
			{
				$this->SetModified();
				$book_data = $book->LoadData();
				$this->cached_data[$id] = $book_data->ListPages();
			}
			unset( $tmp[ $id ] );
		}
		foreach ( $tmp as $id => $to_be_deleted )
		{
			$this->SetModified();
			unset($this->cached_data[$id]);
		}
	}

	protected function GenerateHtml()
	{
		$this->AddControl( "BoxBookNavConfig", I18N("Configure book navigator...") );

		$this->UpdateCache();

		$html = new HtmlTag("ul");
		
		foreach ( $this->cached_data as $book_id => $pages )
		{
			$params = array("_s" => $book_id, "_p" => "" );
			$book = AtomCache::GetAtomData( $book_id );
			$html->Append( $li = new HtmlTag("li") );

			$li->Append( new HtmlLink(  $book->GetTitle(),new Url($params) ) );

			$li->Append( $page_ul = new HtmlTag("ul") );
			if ( is_array($pages ) )
				foreach ( $pages as $page_id => $data )
				{
					if ( !(isset( $data["hide"] ) && $data["hide"]) )
					{
						$params["_p"] = $page_id;
						$page_ul->Append( $page_li = new HtmlTag("li"));
						$page_li->Append( new HtmlLink( $data["title"], new Url($params) ) );
					}
				}
		}
		return $html;
	//	Core::AddJsScript("Engine/plugins/boxes/slideshow/slideshow.js");
	}
}
/**
*Configure the simple navigator.
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Boxes
*@subpackage BoxBookNav
*/
class BoxBookNavConfig extends BoxEdit
{
	protected function BoxEdit( $box )
	{
		$html = new HtmlEditRequest();

	/*	$html->Append( $fs = new HtmlFieldset(I18N("Slideshow delay")) );
		$fs->Append( new HtmlTextInput("","ss_delay", $box->Get("delay")));

		$html->Append( $fs = new HtmlFieldset(I18N("Slideshow fade-in speed")) );
		$fs->Append( new HtmlTextInput("","ss_speed", $box->Get("speed")));

		$html->Append( $fs = new HtmlFieldset(I18N("Images width")) );
		$fs->Append( new HtmlTextInput("","ss_width", $box->Get("width")));

		$html->Append( $fs = new HtmlFieldset(I18N("Images height")) );
		$fs->Append( new HtmlTextInput("","ss_height", $box->Get("height")));

		$html->AppendSubmit(I18N("Confirm"));*/
		return $html;
	}
	protected function BoxSave( $box )
	{
	/*	$text = Core::PostVar("ss_delay", $box->Get("delay"), "string" );
		$box->Set("delay",$text ) && $this->SetReload(true);

		$text = Core::PostVar("ss_speed", $box->Get("speed"), "string" );
		$box->Set("speed",$text ) && $this->SetReload(true);

		$text = Core::PostVar("ss_width", $box->Get("width"), "string" );
		$box->Set("width",$text ) && $this->SetReload(true);

		$text = Core::PostVar("ss_height", $box->Get("height"), "string" );
		$box->Set("height",$text ) && $this->SetReload(true);*/

		return I18N("Slideshow configured!");
	}
}
?>